using System;
using System.Collections.Generic;
using DM.DAO;
using DM.Entity;

namespace DM.Web
{
    public partial class TrangTinTuc : System.Web.UI.Page
    {
        static readonly LoaiTinDAO _loaiTinDao = new LoaiTinDAO();
        static readonly TinTucDAO _tinTucDao = new TinTucDAO();
        private static Int32 lang;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["lang"] == null) lang = Common.TiengViet;
                else lang = ReferenceEquals(Session["lang"], "vi-VN") ? Common.TiengViet : Common.TiengAnh;
                BindingForm();
            }
        }

        private void BindingForm()
        {
            if (Request.QueryString["id"] != null && Request.QueryString["id"] != string.Empty)
            {//Sủ lý tình huống truyền mã tin 
                ViewByID();
            }
            else
            {
                if (Request.QueryString["type"] != null && Request.QueryString["type"] != string.Empty)
                {//Sử lý tình huống truyền loại tin
                    ViewByType();
                }
                else
                {//Sử lý tình huống truyền loại tin gốc
                    ViewByList();
                }
            }
        }

        private void ViewByID()
        {
            //View tintuc chi tiet
            panelViewDetail.Visible = true;
            Int32 maTin = Common.ParseInt32(Request.QueryString["id"]);
            TinTuc tin1 = _tinTucDao.Get(maTin);
            TinTuc tinTuc = _tinTucDao.Gets(tin1.Maloaitin, 1, lang)[0];
            ucViewDetail.Tintuc = tin1;
            

            //Tao mot danh sach cac tin tuc
            LoaiTin loaiTinRoot = _loaiTinDao.GetLoaiTinChaByMaTin(tinTuc.Ma.ToString());
            ViewListUl uc_ViewList = (ViewListUl)LoadControl("UserControl/ViewListUl.ascx");
            if (loaiTinRoot.Ma == (int)RootUnique.Lien_He || loaiTinRoot.Ma == (int)RootUnique.Gioi_Thieu)
            {//Voi nhung tin duy nhat
                List<LoaiTin> listLoaiTin = _loaiTinDao.GetLoaiTin(loaiTinRoot.Ma);
                List<TinTuc> listTin = new List<TinTuc>();
                foreach (LoaiTin loaiTin in listLoaiTin)
                {
                    TinTuc tin = _tinTucDao.Gets(loaiTin.Ma, 1, lang)[0];
                    if (tin.Ma != maTin) listTin.Add(tin);
                }
                uc_ViewList.ListTin = listTin;
            }
            else
            {//Voi nhung tinso nhieu
                List<TinTuc> listTin = _tinTucDao.Gets(tinTuc.Maloaitin, 100, lang);
                List<TinTuc> list = new List<TinTuc>();
                foreach (TinTuc tuc in listTin)
                {
                    if (tuc.Ma != maTin) list.Add(tuc);
                }
                uc_ViewList.ListTin = list;
            }
            if (uc_ViewList.ListTin.Count>0) panelViewList.Controls.Add(uc_ViewList);
        }

        private void ViewByType()
        {
            int maloaiTin = Common.ParseInt32(Request.QueryString["type"]);
            LoaiTin loaitinRoot = _loaiTinDao.getLoaiTinCha(maloaiTin);
            ViewList uc_ViewList = (ViewList)LoadControl("UserControl/ViewList.ascx");
            ViewListUl uc_ViewListUl = (ViewListUl)LoadControl("UserControl/ViewListUl.ascx");
            if (loaitinRoot.Ma == (int)RootUnique.Lien_He || loaitinRoot.Ma == (int)RootUnique.Gioi_Thieu)
            {
                List<LoaiTin> listLoaiTin = _loaiTinDao.GetLoaiTin(loaitinRoot.Ma);
                List<TinTuc> listTin = new List<TinTuc>();
                TinTuc tin = _tinTucDao.Gets(maloaiTin, 1, lang)[0];
                ucViewDetail.Tintuc = tin;
                if (!string.IsNullOrEmpty(tin.Noidung))
                {
                    panelViewDetail.Visible = true;
                    foreach (LoaiTin loaiTin in listLoaiTin)
                    {
                        TinTuc tintuc = _tinTucDao.Gets(loaiTin.Ma, 1, lang)[0];
                        if (tintuc.Ma != tin.Ma && !string.IsNullOrEmpty(tintuc.Noidung)) listTin.Add(tintuc);

                    }
                    if (listTin.Count > 0)
                    {
                        uc_ViewListUl.ListTin = listTin;
                        panelViewList.Controls.Add(uc_ViewListUl);
                    }
                    
                }
            }
            else
            {
                List<TinTuc> listTin = _tinTucDao.Gets(maloaiTin, 100, lang);
                uc_ViewList.ListTin = listTin;
                if (listTin.Count>0 && !string.IsNullOrEmpty(listTin[0].Noidung))
                {
                    panelViewList.Controls.Add(uc_ViewList);
                }
                
            }
            
        }

        private void ViewByList()
        {
            panelViewDetail.Visible = false;
            int loaitinRoot = Common.ParseInt32(Request.QueryString["list"]);
            if (loaitinRoot == (int)RootUnique.Lien_He || loaitinRoot == (int)RootUnique.Gioi_Thieu)
            {
                ViewList uc_ViewList = (ViewList)LoadControl("UserControl/ViewList.ascx");
                List<LoaiTin> listLoaiTin = _loaiTinDao.GetLoaiTin(loaitinRoot);
                List<TinTuc> listTin = new List<TinTuc>();
                foreach (LoaiTin loaiTin in listLoaiTin)
                {
                    TinTuc tin = _tinTucDao.Gets(loaiTin.Ma, 1, lang)[0];
                    if (!string.IsNullOrEmpty(tin.Noidung)) listTin.Add(tin);
                    uc_ViewList.ListTin = listTin;
                    if (uc_ViewList.ListTin.Count > 0)
                        panelViewList.Controls.Add(uc_ViewList);
                }
                
            }
            else
            {
                List<LoaiTin> listLoaiTin = _loaiTinDao.GetLoaiTin(loaitinRoot);
                foreach (LoaiTin loaiTin in listLoaiTin)
                { 
                    List<TinTuc> listTinTuc = _tinTucDao.Gets(loaiTin.Ma, 100, lang);
                    ViewList uc_ViewList = (ViewList) LoadControl("UserControl/ViewList.ascx");
                    if (!string.IsNullOrEmpty(listTinTuc[0].Tieude)) uc_ViewList.ListTin = listTinTuc;
                    if (listTinTuc.Count <= 0 && !string.IsNullOrEmpty(listTinTuc[0].Tieude)) continue;
                    panelViewList.Controls.Add(uc_ViewList);
                }
            }

        }
    }
}